/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.iboss.service.sc;

import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.pagination.PageData;
import net.dgg.dggcloud.service.iboss.dao.sc.UserReadDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FlowUserService extends BaseService {

    @Resource
    private UserReadDao userReadDao;


    /**
     * 根据当前登录人拿到分公司的流程人员List<Map></br>
     *
     * @param map</br>
     * @return 集合</br>
     */
    public List<Map> listFlowUser(Map map) {
        return userReadDao.listFlowUserMap(map);
    }


    public Integer countFlowUserMap(Map map) {
        return userReadDao.countFlowUserMap(map);
    }

    public PageData<Map> scAllProOrderSearchUser(Map params) {
        PageData<Map> result = new PageData<Map>();

        //如果参数对象为空，则创建一个
        Map parameters = params;
        if (parameters == null) {
            parameters = new HashMap();
        }
        //处理页面大小默认值
        if (!parameters.containsKey("pageSize")) {
            parameters.put("pageSize", PageData.DEFAULT_PAGE_SIZE);
        }
        //将当前页码转换为起始条数
        if (!parameters.containsKey("pageIndex")) {
            parameters.put("startCount", 0);
        } else {
//			Integer pageIndex= (Integer) parameters.get("pageIndex");
//			Integer pageSize= (Integer) parameters.get("pageSize");
//			Integer startCount=(pageIndex-1)*pageSize;
            parameters.put("startCount", parameters.get("pageIndex"));
        }
        //rootOrgId默认值处理
        if (!parameters.containsKey("rootOrgId")) {
            parameters.put("rootOrgId", 0);
        }
        //startLevel默认值处理
        if (!parameters.containsKey("startLevel")) {
            parameters.put("startLevel", 0);
        }
        //userState默认值处理
        if (!parameters.containsKey("userState")) {
            parameters.put("userState", 0);
        }

        if (parameters.containsKey("userRoleCodes")) {
            String[] codes = parameters.get("userRoleCodes").toString().split(",");
            StringBuilder expression = new StringBuilder();
            for (String code : codes) {
                expression.append("'");
                expression.append(code);
                expression.append("',");
            }
            if (expression.lastIndexOf(",") == expression.length() - 1) {
                expression.replace(expression.length() - 1, expression.length(), "");
            }
            parameters.put("userRoleCodesArray", expression);
        }

        //组装结果返回
        result.setData(userReadDao.scAllProOrderSearchUser(parameters));
        result.setPageIndex((Integer) parameters.get("pageIndex"));
        result.setPageSize((Integer) parameters.get("pageSize"));
        result.setTotalCount(userReadDao.scAllProOrderSearchUserCount(parameters));

        return result;
    }

}
